package com.carkeeper.system.controller;

import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.system.pojo.Department;
import com.carkeeper.system.service.DepartmentService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 周志雄
 * @version 1.0
 * @date 2020/11/23 20:00
 */
@RestController
@CrossOrigin
@RequestMapping("/dept")
@Api(tags = "部门管理接口api",description = "部门管理接口，提供页面的增、删、改、查")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    /**
     * 更新部门信息
     *
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "接口名update",notes = "更新部门信息",httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "部门名称",required = true,paramType = "body"),
            @ApiImplicitParam(name = "id",value = "部门id",required = true,paramType = "body"),
            @ApiImplicitParam(name = "code",value = "部门编码",required = true,paramType = "body")
    })
    public Result update(@RequestBody Department department) {
        Result result = departmentService.update(department);
        return new Result(true, StatusCode.OK, "更新成功");
    }

    /**
     * 分页+条件查询
     *
     * @param map
     * @return
     */
    @GetMapping
    @ApiOperation(value = "queryByCondition", notes = "分页+条件查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pagesize",value = "每页显示条数", required = true, paramType = "query"),
            @ApiImplicitParam(name = "name",value = "部门名称", required = false, paramType = "query"),
            @ApiImplicitParam(name = "code",value = "部门编号", required = false, paramType = "query")
    })
    public Map queryByCondition(@RequestParam Map map) {
        Map resultMap = departmentService.queryByCondition(map);
        return resultMap;
    }

    /**
     * 添加部门
     *
     * @param department
     * @return
     */
    @PostMapping
    @ApiOperation(value = "add", notes = "添加部门", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "部门名称", required = true, paramType = "body"),
            @ApiImplicitParam(name = "code",value = "部门编号", required = true, paramType = "body")
    })
    public Result add(@RequestBody Department department) {
        Result result = departmentService.add(department);
        return result;
    }

    /**
     * 删除部门
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "delete", notes = "删除部门", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "部门id", required = true, paramType = "path")
    })
    public Result delete(@PathVariable("id") Long id) {
        Result result = departmentService.delete(id);
        return result;
    }


    @GetMapping("/findAll")
    @ApiOperation(value = "findAll", notes = "查询所有部门", httpMethod = "GET")
    public List<Department> findAll() {
        List<Department> list = departmentService.findAll();
        return list;
    }
}
